<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
 *
 * functions.php theme file
 *
 * (c) 2012 by Mike Walsh
 *
 * @author Mike Walsh <mike@walshcrew.com>
 * @package GoLufkin
 * @subpackage functions
 * @version $Revision$
 * @lastmodified $Author$
 * @lastmodifiedby $Date$
 *
 */

/**
 * Initialize the custom header and disable other headers.
 *
 */
$args = array(
	'width'         => 940,
	'height'        => 198,
	'default-image' => get_stylesheet_directory_uri() . '/images/header/GoLufkin_Banner_940x198.png',
	'uploads'       => false,
) ;
add_theme_support('custom-header', $args) ;

/**
 * Add Post Thumbnail Support
 *
 */
function gl_add_post_thumb($title)
{
    return get_the_post_thumbnail(NULL, 'thumbnail') . $title;
}
add_theme_support('post-thumbnails') ;
set_post_thumbnail_size(540, 300, true) ;
add_filter('get_the_excerpt', 'gl_add_post_thumb');
add_image_size('homepage-thumbnail', 300, 200, true) ;

/**
 * Add a home link when a WordPress menu isn't being used
 *
 * @see http://codex.wordpress.org/Template_Tags/wp_page_menu
 */
function gl_childtheme_menu_args($args)
{
    $theme_defaults = array(
        'show_home' => 'Home',
        'sort_column' => 'menu_order',
        'menu_class' => 'menu',
        'echo' => true,
    );
	$args = wp_parse_args( $args, $theme_defaults );
    
	return $args;
}
add_filter('wp_page_menu_args','gl_childtheme_menu_args');

/**
 * Build Page Category Meta Box to allow the user to select
 * a single category for the page.  This category and custom
 * field will be use to drive a custom loop on a page template.
 *
 */
function gl_page_category_mb()
{
    add_meta_box('gl-page-category-meta',
        'Page Category', 'gl_page_category_meta', 'page', 'side', 'high') ;
}
add_action('add_meta_boxes', 'gl_page_category_mb') ;

/**
 * Build the Page Catgory form that will reside in the Meta Box.
 *
 */
function gl_page_category_meta($page)
{
	$msg = '<div class="misc-pub-section">Select a category to be used by the
	    <b><i>Category</i></b> page template.  Setting the category to <b><i>
        No Category Assigned</i></b> will remove the Page Category.</div>' ;

    //  Has a page category already been assigned?  If so, get it.
    $page_cat = get_post_meta($page->ID, '_gl_page_cat', true) ;

    //  Set the argument list to construct the categories drop down
    $args = array(
        'orderby' => 'name'
       ,'name' => 'gl_page_cat'
       ,'selected' => ($page_cat === '') ? 0 : $page_cat
       ,'show_option_none' => __('No Category Assigned')
       ,'hide_empty' => 0
    ) ;
    ?>
    <p>Category:
    <?php wp_dropdown_categories($args) ; ?>
    </p>
    <?php
    echo __($msg) ;

}
add_action('save_post', 'gl_save_page_category') ;

/**
 * Process the Page Category from the Meta Box.  If
 * no Page Category is assigned, then remove any value
 * that may have been set previously.
 *
 */
function gl_save_page_category($page_id)
{
    //  Verify meta data was set
    if (isset($_POST['gl_page_cat']))
    {
        $cat_id = intval($_POST['gl_page_cat']) ;

        //  Save the page category id or delete it

        if ($cat_id === -1)
            delete_post_meta($page_id, '_gl_page_cat') ;
        else
            update_post_meta($page_id, '_gl_page_cat',
                intval($_POST['gl_page_cat'])) ;
    }
}

/**
 * Return the category slug for a category id.
 *
 * @return string - category slug text
 */
function gl_get_cat_slug($cat_id)
{
	$cat_id = (int) $cat_id ;
	$category = &get_category($cat_id) ;
	return $category->slug ;
}

/**
 * If a page has been assigned a category, add a body class
 * based on the category slug so the page can be easily styled.
 *
 * @param mixed - existing classes
 * @return mixed - updated classes
 */
function gl_body_class($classes)
{
    global $post ;

    $cat_id = get_post_meta($post->ID, '_gl_page_cat', true) ;

    if ($cat_id !== "")
    {
        $cat_slug = gl_get_cat_slug($cat_id) ;
        $classes[] = 'page-category-' . $cat_slug ;
    }

    return $classes ;
}
add_filter('body_class', 'gl_body_class') ;

/**
 * Add class names for each post based on their category names
 * post can be easily be uniquely styled.
 *
 * @param mixed - existing classes
 * @return mixed - updated classes
 */
function gl_category_name_class($classes)
{
    global $post ;

    foreach((get_the_category($post->ID)) as $category)
        $classes[] = $category->category_nicename ;

    return $classes ;
}
add_filter('post_class', 'gl_category_name_class') ;
add_filter('body_class', 'gl_category_name_class') ;

/**
 * Action to add theme credits
 *
 */
function gl_credits()
{
    $txt = sprintf('<div id="golufkin-footer">Copyright &copy; 2011-%s, %s - All rights reserved.<br>',
        date('Y'), get_bloginfo('name')) ;
    $txt .= '<span id="golufkin-link">Lufkin Road Middle School | 1002 Lufkin Road, Apex NC 27539 | phone 919.387.4465</span><br/>' ;

    $txt .= 'The <a href="http://www.michaelwalsh.org/wordpress-themes/golufkin-theme/" title="GoLufkin">GoLufkin</a> theme (vMAJOR_RELEASE.MINOR_RELEASE.$WCREV$) is a child of the <a href="http://theme.wordpress.com/themes/twentyten/">Twenty Ten</a> theme.</div>' ;

    echo $txt ;
}
add_action('twentyten_credits', 'gl_credits') ;

/**  WooCommerce extensions  **/

/**
 *
 */
//add_action('woocommerce_order_details_after_order_table', 'gl_woocommerce_order_details_after_order_table') ;
function gl_woocommerce_order_details_after_order_table($order)
{
    printf('<h2>%s::%s</h2>', basename(__FILE__), __LINE__);
    print '<pre>';
    print_r($order) ;
    print '</pre>';
}

//add_action('woocommerce_thankyou', 'gl_woocommerce_thankyou') ;
function gl_woocommerce_thankyou($order)
{
    printf('<h2>%s::%s</h2>', basename(__FILE__), __LINE__);
    print_r($order) ;
}

/**
 * Add the field to the checkout
 *
 */
add_action('woocommerce_before_order_notes', 'gl_custom_checkout_field');
 
function gl_custom_checkout_field( $checkout )
{
    printf('<div id="gl_custom_checkout_field"><h3>%s</h3><br/ style="clear:both;">', __('Student Information'));
 
    woocommerce_form_field( 'gl_student_name', array(
        'type'          => 'text',
        'class'         => array('gl-field-class form-row-wide'),
        'label'         => __('Student Name'),
        'placeholder'       => __('Enter Student\'s Name'),
        ), $checkout->get_value( 'gl_student_name' ));
 
    woocommerce_form_field( 'gl_student_grade', array(
        'type'          => 'text',
        'class'         => array('gl-field-class form-row-wide'),
        'label'         => __('Student Grade'),
        'placeholder'       => __('Enter Student\'s Grade'),
        ), $checkout->get_value( 'gl_student_grade' ));
 
    woocommerce_form_field( 'gl_student_track', array(
        'type'          => 'text',
        'class'         => array('gl-field-class form-row-wide'),
        'label'         => __('Student Track'),
        'placeholder'       => __('Enter Student\'s Track'),
        ), $checkout->get_value( 'gl_student_track' ));
 
    echo '</div>';
}

/**
 * Process the checkout
 *
 */
add_action('woocommerce_checkout_process', 'gl_custom_checkout_field_process');
 
function gl_custom_checkout_field_process()
{
    global $woocommerce;
 
    // Check if set, if its not set add an error.
    if (!$_POST['gl_student_name'])
         $woocommerce->add_error( __('Please enter student\'s name.') );
 
    // Check if set, if its not set add an error.
    if (!$_POST['gl_student_grade'])
         $woocommerce->add_error( __('Please enter student\'s grade.') );
 
    // Check if set, if its not set add an error.
    if (!$_POST['gl_student_track'])
         $woocommerce->add_error( __('Please enter student\'s track.') );
}

/**
 * Update the order meta with field value
 *
 */
add_action('woocommerce_checkout_update_order_meta', 'gl_custom_checkout_field_update_order_meta');
 
function gl_custom_checkout_field_update_order_meta( $order_id )
{
    if ($_POST['gl_student_name'])
        update_post_meta( $order_id, 'Student Name', esc_attr($_POST['gl_student_name']));

    if ($_POST['gl_student_grade'])
        update_post_meta( $order_id, 'Student Grade', esc_attr($_POST['gl_student_grade']));

    if ($_POST['gl_student_track'])
        update_post_meta( $order_id, 'Student Track', esc_attr($_POST['gl_student_track']));
}

/**
 * Add order meta to email templates
 *
 */
add_action('woocommerce_email_after_order_table', 'gl_email_student_details');
add_action('woocommerce_order_details_after_order_table', 'gl_email_student_details');
function gl_email_student_details( $order )
{
    printf('<h2>%s</h2>', __('Student details', 'woocommerce'));

    $sn = get_post_meta( $order->id, 'Student Name', true ) ;
    $sg = get_post_meta( $order->id, 'Student Grade', true ) ;
    $st = get_post_meta( $order->id, 'Student Track', true ) ;

    printf('<p><strong>%s:</strong>  %s</p>', __('Name', 'woocommerce'), $sn) ;
    printf('<p><strong>%s:</strong>  %s</p>', __('Grade', 'woocommerce'), $sg) ;
    printf('<p><strong>%s:</strong>  %s</p>', __('Track', 'woocommerce'), $st) ;
}

?>
